/*
Content: Preparation of terrorist attacks data for PASS analysis
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Monteiro
File version: 1.1 - May 8th, 2025
Stata version: 
*/


********************************************************************************
*** GLOBALS FOR THE ANALYSIS
********************************************************************************
global soep_corigin = "soep_corigin_id"
global ieb_corigin = "ieb_nat_id"
global pass_corigin = "ogeblandna"

********************************************************************************
*** Country identifiers 
********************************************************************************

preserve

foreach type in terror  {

import excel "$codes\countrycodes_ieb3.xlsx", clear sheet("`type'") firstrow

*replace country_name = strtrim(country_name) 

save "$dataevent/countrycodes_pass_`type'.dta", replace

}

restore

*************************************************************
** GTD: MONTH-YEAR-COUNTRY OF ORIGIN TERRORIST ATTACKS ******
*************************************************************

import delimited "$dataevent/globalterror_1970_2020.csv", clear   // updated data up to 2020

append using "$codes\terror_dd_formeryugos.dta", generate(yugo_countries) // use former yugoslavia to have backward info on countries
append using "$codes\terror_dd_countriesyugo.dta", generate(countries_yugo) // use countries to compute forward events of yugoslavia
append using "$codes\terror_dd_easternbloc.dta", generate(countries_eastern) // use countries to compute forward and backward events for the former eastern bloc (from post-soviet countries)

* Date of event 

* Complete date of the event
gen startdate = mdy(imonth,iday,iyear)	

* Month-year of the event
gen startmy = ym(iyear,imonth) 
format startmy %tm 

rename imonth month
rename iyear year
rename iday day 


* Indicator for each event

gen gtd = 1
clonevar terror = gtd
clonevar terror_suc = success


* Target type
cap drop targtype
gen targtype = 1 if inlist(targtype1, 1,13,14,17,18,20) 					// Private and business (non-state militias are also included, which might be a problem, but they dont fit)
replace targtype = 2 if inlist(targtype1, 2,3,4,7,22)						// political actors and police/military
replace targtype = 3 if inlist(targtype1, 6,9,11,16,19,21,5,8,10,12,15)		// Infrastructures and other institutions


lab def targtype 1"Private and business" 2"Political actors and military" 3"Infrastructures and others"
lab val targtype targtype

* Total number of affected 

rename nkill nkill_terror
rename nwound nwound_terror

gen naffect_terror = nkill_terror + nwound_terror

foreach var in 1 10 30 50 {

g terror_`var'k=0
replace terror_`var'k=1 if nkill_terror>=`var' & nkill_terror!=.
}

foreach var in 1 10 30 50 {

g terror_`var'aff=0
replace terror_`var'aff=1 if naffect_terror>=`var' & naffect_terror!=.
}




*  Fix country code variable 	

rename country_txt country_name 
rename country country_code 

sort country_code

tab country_name if yugo_countries==1
tab country_name if countries_yugo==1

replace country_name = "Eswatini" if inlist(country_name,"Swaziland") 
replace country_name = "Russia" if inlist(country_name,"Soviet Union") 
replace country_name = "Côte d'Ivoire" if inlist(country_name,"Ivory Coast") 
replace country_name = "Italy" if inlist(country_name,"Vatican City")



preserve 
use "$dataevent/countrycodes_pass_terror.dta", clear

bys country_code: gen unique = 1 if _n == 1
keep if unique == 1
drop unique

tempfile countrycodes_pass_terror

save `countrycodes_pass_terror'

restore

* Merge
merge m:1 country_code using `countrycodes_pass_terror', keep(3) 


* Label variables 

lab var startmy "Month-year of the event"
lab var day "Day of the event"
lab var month "Month of the event"
lab var year "Year of the event"
lab var startdate "Complete date of event start"
lab var gtd "Indicator for any event in the gtd data"
lab var terror "Indicator for any terror event in the gtd data"
lab var nkill_terror "Number of killed"
lab var nwound_terror "Number of wounded"
lab var naffect_terror "Number of affected (killed + wounded)"
lab var targtype "Type of target of the terror attack"
lab var success "Successful attack"
lab var country_name "Country name"
lab var country_code "Country code in gtd" 

* Collapse at the daily-country of origin level

preserve 
foreach v of var * {
local l`v' : variable label `v'
if `"`l`v''"' == "" {
local l`v' "`v'"
}
}

collapse (sum) terror_suc terror_*aff terror_*k terror nkill_terror nwound_terror naffect_terror success (mean) gtd (firstnm) country_code targtype country_name year month day startmy, by(${pass_corigin} startdate) 
drop if startdate==.

foreach v of var * {
label var `v' `"`l`v''"'
}



save "$dataevent/terror_daily_gtd_pass_v1.dta", replace
restore


* Collapse at the monthly-country of origin level
preserve 
foreach v of var * {
local l`v' : variable label `v'
if `"`l`v''"' == "" {
local l`v' "`v'"
}
}

collapse (sum) terror_suc terror_*aff terror_*k terror nkill_terror nwound_terror naffect_terror success (mean) gtd (firstnm) country_code targtype country_name year month, by(${pass_corigin} startmy) 

foreach v of var * {
label var `v' `"`l`v''"'
}

save "$dataevent/terror_monthly_gtd_pass_v1.dta", replace
drop if year > 2021
tabstat terror_suc, by(year)
sum terror_suc
restore





